<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
        :where="defalutWhere"
      >
        <template #toolbar="{}">
          <el-button type="info" @click="exportExcel" :icon="DocumentCopy">
            导出表格
          </el-button>
        </template>
        <template #plate_number="{ row }">
          <span class="blue pointer" @click="openDetail(row)">{{
            row.plate_number
          }}</span>
        </template>
        <template #insurance_date_b="{ row }">
          <span class="blue pointer" @click="openInsurance(row, '交强险')">
            {{ row.insurance_date_b }}<br />
            <span class="red"> {{ row.bDay }} </span>
          </span>
        </template>
        <template #insurance_date_c="{ row }">
          <span class="blue pointer" @click="openInsurance(row, '交强险')">
            {{ row.insurance_date_c }}<br />
            <span class="red"> {{ row.cDay }} </span>
          </span>
        </template>
        <template #insurance_offer="{ row }">
          <span class="blue pointer" @click="openOfferList(row)">{{
            row.insurance_offer
          }}</span>
        </template>

        <template #action="{ row }">
          <el-button-group>
            <el-button type="primary" size="small" @click="openOffer(row)">
              报价
            </el-button>
            <el-button
              type="warning"
              size="small"
              @click="openInsurancApply(row)"
            >
              报价申请
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>

    <!-- 车辆详情 -->
    <detail v-model="showDetail" :data="current" />
    <!-- 车辆保险信息 -->
    <insurance v-model="showInsurance" :data="current" :cate="currentCate" />
    <create
      v-model="showOffer"
      :vehicle-id="vehicleId"
      :plate-number="plateNumber"
      @done="reload"
    />
    <!-- 报价列表 -->
    <offer-list v-model="showOfferList" :data="current" />
    <!-- 报价申请 -->
    <insurancePlan v-model="showInsurancePlan" :data="current" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './components/search.vue';
  import {
    pageVehicleEndList,
    exportVehicleInsuranceInTheEnd
  } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';
  import { DocumentCopy } from '@element-plus/icons-vue';
  import Create from '@/views/insurance/components/create.vue';
  import Detail from '../components/detail.vue';
  import OfferList from '../components/offer-list.vue';
  import Insurance from '../components/insurance.vue';
  import insurancePlan from '@/views/insurance/components/create-plan.vue';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 列表的列 */
  const columns = ref([
    {
      prop: 'id',
      label: 'ID',
      width: 85,
      align: 'center'
    },
    {
      width: 70,
      align: 'center',
      prop: 'area',
      label: '区域'
    },
    {
      width: 80,
      align: 'center',
      prop: 'status',
      label: '状态'
    },
    {
      width: 110,
      align: 'center',
      slot: 'plate_number',
      label: '车牌号码'
    },
    {
      prop: 'company',
      label: '车队'
    },
    {
      width: 100,
      prop: 'realname',
      label: '车主'
    },
    {
      width: 120,
      prop: 'tel',
      label: '联系电话'
    },
    {
      width: 110,
      align: 'center',
      slot: 'insurance_date_c',
      label: '交强险到期'
    },
    {
      width: 110,
      align: 'center',
      slot: 'insurance_date_b',
      label: '商业险到期'
    },
    {
      width: 100,
      align: 'center',
      slot: 'insurance_offer',
      label: '报价单'
    },

    {
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      width: 180,
      align: 'center',
      fixed: 'right'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  const defalutWhere = ref({
    status: '正常',
    deadlineType: '交强险'
  });
  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageVehicleEndList({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 报价申请 */
  const showInsurancePlan = ref(false);
  const openInsurancApply = (row) => {
    current.value = row ?? null;
    showInsurancePlan.value = true;
  };

  /** 报价列表 */
  const showOfferList = ref(false);
  const openOfferList = (row) => {
    current.value = row;
    showOfferList.value = true;
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 车辆详情 */
  const showDetail = ref(false);
  const openDetail = (row) => {
    current.value = row ?? null;
    showDetail.value = true;
  };

  /** 报价 */
  const showOffer = ref(false);
  const vehicleId = ref(0);
  const plateNumber = ref('');

  const openOffer = (row) => {
    vehicleId.value = row.id;
    plateNumber.value = row.plate_number;
    showOffer.value = true;
  };

  /** 保险信息 */
  const showInsurance = ref(false);
  const currentCate = ref('全部');
  const openInsurance = (row, cate) => {
    current.value = row ?? null;
    currentCate.value = cate;
    showInsurance.value = true;
  };

  /** 导出表格 */
  const exportExcel = () => {
    const loading = EleMessage.loading('请求中..');
    tableRef.value?.fetch(({ where }) => {
      // 方法的参数与数据源的完全一致
      // 请求查询全部接口
      console.log(where);
      var requestData = where;
      requestData.limit = 99999999;
      requestData.page = 1;

      exportVehicleInsuranceInTheEnd(requestData)
        .then((res) => {
          loading.close();
          window.open(res);
        })
        .catch(() => {
          loading.close();
        });
    });
  };
</script>
